//
//  JDReleaseUnionNewsViewController.swift
//  DragonWallet
//
//  Created by volantPig on 2024/5/21.
//

import UIKit
import SwiftyJSON
/// 发布 圈内商讯
class JDReleaseUnionNewsViewController: JDBaseViewController {
    
    var bottomView: UIView?
    var backButton: UIButton?
    var releaseButton: UIButton?
    
    var myTableView: UITableView?
    
    var messageInputCell: JDMessageInputCell?
    
    var imgNumber: Int = 0
    var imgButtonOne: UICloseButton?
    var imgButtonTwo: UICloseButton?
    var imgButtonThree: UICloseButton?
    
    var videoButton: UICloseButton?
    
    var imgOneUrlStr: String?
    var imgTwoUrlStr: String?
    var imgThreeUrlStr: String?
    var videoUrlPath: String?
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.setupConfig()
        self.setupUIElements()
        
        NotificationCenter.default.addObserver(self, selector: #selector(payResult(_:)), name: NSNotification.Name("payResult"), object: nil)
        // Do any additional setup after loading the view.
    }
    
    @objc func payResult(_ notify: NSNotification) {
       print("处理支付结果222")
       let payResultType = notify.object as? String
       if payResultType == "成功" {
           
           let orderList = JDOrderListViewController()
           orderList.isFromPaySuccess = true
           orderList.currentIndex = 3
           self.hidesBottomBarPushViewController(orderList)
           
       } else if payResultType == "失败" || payResultType == "取消" {
           let orderList = JDOrderListViewController()
           orderList.isFromPaySuccess = true
           orderList.currentIndex = 0
           self.hidesBottomBarPushViewController(orderList)
           
       }
       print("处理结果吧")
       
   }
    
    
    func setupConfig() {
        self.setupNavigationTitle("我要发布推荐")
        self.removeSeparatorLine()
        self.setupLeftBackItem()
    }
    
    func setupUIElements() {
        
        self.bottomView = UIView()
        self.view.addSubview(self.bottomView!)
        self.bottomView?.snp.makeConstraints({ make in
            make.left.right.bottom.equalTo(self.view)
            make.height.equalTo(104)
        })
        self.bottomView?.backgroundColor = .white
        
        self.backButton = UIButton(type: .custom)
        self.bottomView?.addSubview(self.backButton!)
        self.backButton?.snp.makeConstraints({ make in
            make.centerY.equalTo(self.bottomView!).offset(-17)
            make.left.equalTo(self.bottomView!).offset(12)
            make.right.equalTo(self.bottomView!.snp.centerX).offset(-5)
            make.height.equalTo(48)
        })
        self.backButton?.setTitle("返回", for: .normal)
        self.backButton?.setTitleColor(.black, for: .normal)
        self.backButton?.titleLabel?.font = UIFont.systemFont(ofSize: 16, weight: .regular)
        self.backButton?.backgroundColor = .init(hexString: "#F5F6F8")
        self.backButton?.layer.cornerRadius = 24
        self.backButton?.addTarget(self, action: #selector(backAction), for: .touchUpInside)
        
        self.releaseButton = UIButton(type: .custom)
        self.bottomView?.addSubview(self.releaseButton!)
        self.releaseButton?.snp.makeConstraints({ make in
            make.left.equalTo(self.bottomView!.snp.centerX).offset(5)
            make.centerY.equalTo(self.bottomView!).offset(-17)
            make.height.equalTo(48)
            make.right.equalTo(self.bottomView!).offset(-12)
        })
        self.releaseButton?.setTitle("发布尨商圈", for: .normal)
        self.releaseButton?.setTitleColor(.white, for: .normal)
        self.releaseButton?.titleLabel?.font = UIFont.systemFont(ofSize: 16, weight: .regular)
        self.releaseButton?.backgroundColor = .init(hexString: "#FA6161")
        self.releaseButton?.layer.cornerRadius = 24
        self.releaseButton?.addTarget(self, action: #selector(releaseAction), for: .touchUpInside)
        
        
        self.myTableView = UITableView(frame: .zero, style: .plain)
        self.view.addSubview(self.myTableView!)
        self.myTableView?.snp.makeConstraints({ make in
            make.left.right.equalTo(self.view)
            make.bottom.equalTo(self.bottomView!.snp.top)
            make.top.equalTo(self.view).offset(statusBarHeight+44)
        })
        self.myTableView?.delegate = self
        self.myTableView?.dataSource = self
        self.myTableView?.register(UITableViewCell.self, forCellReuseIdentifier: "emptyCell")
        self.myTableView?.register(JDMessageInputCell.self, forCellReuseIdentifier: "messageInputCell")
        self.myTableView?.backgroundColor = .init(hexString: "#F5F6F8")
        self.myTableView?.separatorStyle = .none
    }
    
    
    /*
     // MARK: - Navigation
     
     // In a storyboard-based application, you will often want to do a little preparation before navigation
     override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
     // Get the new view controller using segue.destination.
     // Pass the selected object to the new view controller.
     }
     */
    
    @objc func selectedImgV(_ currentButton: UIButton) {
        
        
        self.openAlbumAlert {[weak self] selectedType, images in
            if selectedType == .photoCamera {
                guard let imgV = images.first else { return }
                if currentButton == self?.imgButtonOne {
                    DispatchQueue.main.async {
                        self?.imgButtonOne?.setImage(imgV, for: .normal)
                        self?.imgButtonTwo?.isHidden = false
                    }
                    self?.uploadImgV(imgV, { urlStrDic in
                        self?.imgOneUrlStr = JSON(urlStrDic?.values.first as Any).stringValue
                        print("第一张图片地址:\(urlStrDic)")
                    })
                } else if currentButton == self?.imgButtonTwo {
                    self?.imgButtonTwo?.setImage(imgV, for: .normal)
                    self?.imgButtonThree?.isHidden = false
                    self?.uploadImgV(imgV, { urlStrDic in
                        self?.imgTwoUrlStr = JSON(urlStrDic?.values.first as Any).stringValue
                        print("第二张图片地址:\(urlStrDic)")
                    })
                } else if currentButton == self?.imgButtonThree {
                    self?.imgButtonThree?.setImage(imgV, for: .normal)
                    self?.uploadImgV(imgV, { urlStrDic in
                        self?.imgThreeUrlStr = JSON(urlStrDic?.values.first as Any).stringValue
                        print("第三张图片地址:\(urlStrDic)")
                    })
                }
            }  else if selectedType == .photoAblum {
//                for (index, subImage) in images.enumerated() {
//                    self?.uploadImgV(subImage, { urlStrDic in
//                        if index == 0 {
//                            DispatchQueue.main.async {
//                                self?.imgButtonOne?.setImage(subImage, for: .normal)
//                                self?.imgButtonTwo?.isHidden = false
//                                self?.imgOneUrlStr = JSON(urlStrDic?.values.first as Any).stringValue
//                            }
//                        } else if index == 1 {
//                            Task {
//                                self?.imgButtonTwo?.setImage(subImage, for: .normal)
//                                self?.imgButtonThree?.isHidden = false
//                                self?.imgTwoUrlStr = JSON(urlStrDic?.values.first as Any).stringValue
//                            }
//                        } else if index == 2 {
//                            Task {
//                                self?.imgButtonThree?.setImage(subImage, for: .normal)
//                                self?.imgThreeUrlStr = JSON(urlStrDic?.values.first as Any).stringValue
//                            }
//                        }
//                    })
//                }
                
                
                // TODO: 这里需要处理
                guard let imgV = images.first else { return }
                if currentButton == self?.imgButtonOne {
                    DispatchQueue.main.async {
                        self?.imgButtonOne?.setImage(imgV, for: .normal)
                        self?.imgButtonTwo?.isHidden = false
                    }
                    self?.uploadImgV(imgV, { urlStrDic in
                        self?.imgOneUrlStr = JSON(urlStrDic?.values.first as Any).stringValue
                        print("第一张图片地址:\(urlStrDic)")
                    })
                } else if currentButton == self?.imgButtonTwo {
                    self?.imgButtonTwo?.setImage(imgV, for: .normal)
                    self?.imgButtonThree?.isHidden = false
                    self?.uploadImgV(imgV, { urlStrDic in
                        self?.imgTwoUrlStr = JSON(urlStrDic?.values.first as Any).stringValue
                        print("第二张图片地址:\(urlStrDic)")
                    })
                } else if currentButton == self?.imgButtonThree {
                    self?.imgButtonThree?.setImage(imgV, for: .normal)
                    self?.uploadImgV(imgV, { urlStrDic in
                        self?.imgThreeUrlStr = JSON(urlStrDic?.values.first as Any).stringValue
                        print("第三张图片地址:\(urlStrDic)")
                    })
                }
                
            }
        }
        
        
//        printLog("选择照片")
//        self.openImgagePickerVC()
//        self.pictureSelectedCompleted = {[weak self] imgV in
//            if currentButton == self?.imgButtonOne {
//                DispatchQueue.main.async {
//                    self?.imgButtonOne?.setImage(imgV, for: .normal)
//                    self?.imgButtonTwo?.isHidden = false
//                }
//                self?.uploadImgV(imgV, { urlStrDic in
//                    self?.imgOneUrlStr = JSON(urlStrDic?.values.first).stringValue
////                    "https://img2.baidu.com/it/u=2239350219,2012909916&fm=253&fmt=auto&app=138&f=JPEG?w=800&h=1066"//urlStr
//                
//                    print("第一张图片地址:\(self?.imgOneUrlStr)")
//                })
//            } else if currentButton == self?.imgButtonTwo {
//                self?.imgButtonTwo?.setImage(imgV, for: .normal)
//                self?.imgButtonThree?.isHidden = false
//                self?.uploadImgV(imgV, { urlStrDic in
//                    self?.imgTwoUrlStr = JSON(urlStrDic?.values.first).stringValue
////                    "https://img0.baidu.com/it/u=2191392668,814349101&fm=253&fmt=auto&app=138&f=JPEG?w=800&h=1399"//urlStr
//                    print("第二张图片地址:\(self?.imgTwoUrlStr)")
//                })
//            } else if currentButton == self?.imgButtonThree {
//                self?.imgButtonThree?.setImage(imgV, for: .normal)
//                self?.uploadImgV(imgV, { urlStrDic in
//                    self?.imgThreeUrlStr = JSON(urlStrDic?.values.first).stringValue
////                "https://wx1.sinaimg.cn/mw690/b3e366e1gy1hrd8ptp3l7j20u00u00um.jpg"//urlStr
//                    print("第三张图片地址:\(self?.imgThreeUrlStr)")
//                })
//            }
//        }
    }
    
    @objc func selectedMainBoardVideo() {
        self.selectedVideo()
        //        self.videoSelectedCompleted = { [weak self] urlStr, firstImg in
        //
        //            let outputPath = NSHomeDirectory() + "/Documents/\(Date().timeIntervalSince1970).mp4"
        //
        //            JDTools.transformMoive(inputPath: urlStr, outputPath: outputPath) { videoLoacelUrl in
        //                DispatchQueue.main.async {
        //                    self?.videoButton?.setImage(firstImg, for: .normal)
        //                }
        //                self?.uploadVideo(videoLoacelUrl, { videoNetUrlPath in
        //
        //                    self?.videoUrlPath = videoNetUrlPath
        //                    printLog("video的url:\(videoNetUrlPath)")
        //                })
        //            }
        //        }
        
    }
    
    @objc func releaseAction() {
        
        // 发布圈内商讯
        
        var params = [String: Any]()
        params["content"] = self.messageInputCell?.inputTextView?.text
        if (self.imgOneUrlStr?.count ?? 0) > 0 {
            params["picUrl1"] = self.imgOneUrlStr
        }
        
        if (self.imgTwoUrlStr?.count ?? 0) > 0 {
            params["picUrl2"] = self.imgTwoUrlStr
        }
        
        if (self.imgThreeUrlStr?.count ?? 0) > 0 {
            params["picUrl3"] = self.imgThreeUrlStr
        }
        self.showLoading()
        JDNetWorkRequest(.releaseUnionNews(params)) { responseStr in
            self.dismissLoading()
            printLog("发布成功:\(responseStr)")
            
//            {
//              "code": 0,
//              "msg": "ok",
//              "data": {
//                "orderNo": "string",
//                "orderTime": "string"
//              }
//            }
            
            let commonModel = JDCommonModel.deserialize(from: responseStr, designatedPath: "")
            if commonModel?.code == "0" {
                // 成功
                
                let alipayStr = JSON(commonModel?.data as Any).dictionaryValue["orderNo"]?.stringValue ?? ""
                let payPrice = JSON(commonModel?.data as Any).dictionaryValue["totalAmount"]?.doubleValue ?? 0.00
                let payPriceStr = String(format: "%.2f", payPrice/1000.0)
                self.continuePayAction(alipayStr,payPriceStr)
                
                
//                let alipayStr = JSON(commonModel?.data as Any).dictionaryValue["orderStr"]?.stringValue ?? ""
//                JDAlipayManager.pay(alipayStr)
                
//                JDTools.getKeyWindows()?.toast(message: "发布成功")
//                self.navigationController?.popViewController(animated: true)
            }
//            {"msg":"ok","code":0}
            
        } failure: { error in
            self.dismissLoading()
            printLog("发布错误:\(error)")
        } error: { errorCode in
            self.dismissLoading()
            printLog("错误code:\(errorCode)")
        }
    }
    
    @objc func removeImgV(index: Int) {
        
        if index == 0 {
            
            if (self.imgThreeUrlStr?.count ?? 0) > 0 {
                // 有第三个
                if let imgTwo = self.imgButtonTwo?.currentImage {
                    self.imgOneUrlStr = self.imgTwoUrlStr
                    self.imgButtonOne?.setImage(imgTwo, for: .normal)
                }
                if let imgThree = self.imgButtonThree?.currentImage {
                    self.imgTwoUrlStr = self.imgThreeUrlStr
                    self.imgButtonTwo?.setImage(imgThree, for: .normal)
                }
                
                self.imgThreeUrlStr = nil
                self.imgButtonThree?.refrehPlaceholder()
                
            } else {
                // 没有第三个
                if (self.imgTwoUrlStr?.count ?? 0) > 0 {
                    // 有第二个
                    if let imgTwo = self.imgButtonTwo?.currentImage {
                        self.imgOneUrlStr = self.imgTwoUrlStr
                        self.imgTwoUrlStr = nil
                        self.imgButtonOne?.setImage(imgTwo, for: .normal)
                        self.imgButtonTwo?.refrehPlaceholder()
                        self.imgButtonThree?.isHidden = true
                    }
                } else {
                    // 没有第二个
                    self.imgOneUrlStr = nil
                    self.imgButtonOne?.refrehPlaceholder()
                    self.imgButtonTwo?.isHidden = true
                }
            }
        } else if index == 1 {
            
            // 有第三个   删除第二个   需要退一个位置
            if (self.imgThreeUrlStr?.count ?? 0) > 0 {
                
                if let imgThree = self.imgButtonThree?.currentImage {
                    self.imgTwoUrlStr = self.imgThreeUrlStr
                    self.imgThreeUrlStr = nil
                    self.imgButtonTwo?.setImage(imgThree, for: .normal)
                    self.imgButtonThree?.refrehPlaceholder()
                }
            } else {
                // 没有第三个，但是第三个  应该已经显示了
                self.imgButtonThree?.isHidden = true
                self.imgTwoUrlStr = nil
                self.imgButtonTwo?.refrehPlaceholder()
                
            }

        } else if index == 2 {
            self.imgThreeUrlStr = nil
            self.imgButtonThree?.refrehPlaceholder()
        }
    }
     
    
    @objc func continuePayAction(_ orderNo: String?, _ priceStr: String?) {
        
        guard let currentOrderNo = orderNo else { return  }
        
        var params = [String: Any]()
        params["orderNo"] = currentOrderNo
                    
        JDNetWorkRequest(.doOrderPay(params)) { responseStr in
            
            let paymentMethodView = JDPaymentMethodView()
            paymentMethodView.priceLabel?.text = String(format: "￥%@", priceStr ?? "0.00") //"￥100000000000.00"
            paymentMethodView.priceLabel?.priceString(UIFont.systemFont(ofSize: 18, weight: .semibold), UIFont.systemFont(ofSize: 36, weight: .semibold))
            paymentMethodView.confirmButtonSelected = { [weak self] in
                print("看看这个接口吧:\(responseStr)")
                let commonModel = JDCommonModel.deserialize(from: responseStr, designatedPath: "")
                if commonModel?.code == "0" {
                    let alipayStr = JSON(commonModel?.data as Any).dictionaryValue["orderStr"]?.stringValue ?? ""
                    JDAlipayManager.pay(alipayStr)
                    paymentMethodView.removeFromSuperview()
                }
            }
            paymentMethodView.cancelCallBack = { [weak self] in
                
                let orderList = JDOrderListViewController()
                orderList.isFromPaySuccess = true
                orderList.currentIndex = 0
                self?.hidesBottomBarPushViewController(orderList)
                
                
//                @objc func payResult(_ notify: NSNotification) {
//                    print("处理支付结果111")
//                    self.payingVC.dismiss(animated: false)
//                    
//                    let payResultType = notify.object as? String
//                    if payResultType == "成功" {
//                        let resultVC = JDPayResultViewController()
//                        resultVC.paySuccess = true
//                        self.hidesBottomBarPushViewController(resultVC)
//                        
//                    } else if payResultType == "失败" {
//                        let resultVC = JDPayResultViewController()
//                        resultVC.paySuccess = false
//                        self.hidesBottomBarPushViewController(resultVC)
//                        
//                    } else if payResultType == "取消" {
//                        let resultVC = JDPayResultViewController()
//                        resultVC.paySuccess = false
//                        resultVC.payResultStr = "用户取消支付"
//                        self.hidesBottomBarPushViewController(resultVC)
//                    }
//                    
//                    print("处理结果吧")
//                    
//                }
                
                
                
                
                
                
                
            }
            
            JDTools.getKeyWindows()?.rootViewController?.view.addSubview(paymentMethodView)

            
        } failure: { error in
            print("付款错误:\(error)")
            
        } error: { errorCode in
            print("付款异常code:\(errorCode)")
        }
        
    }
    
}

extension JDReleaseUnionNewsViewController: UITableViewDelegate, UITableViewDataSource {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 4
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        if indexPath.row == 0 {
            if self.messageInputCell == nil {
                self.messageInputCell = tableView.dequeueReusableCell(withIdentifier: "messageInputCell") as? JDMessageInputCell
                self.messageInputCell?.inputTitle?.text = "项目说明"
                self.messageInputCell?.inputTextView?.placeholder = "请输入项目说明"
                self.messageInputCell?.inputTextView?.snp.updateConstraints({ make in
                    make.height.equalTo(83)
                })
            }
            
            return self.messageInputCell!
        }
        
        let cell = tableView.dequeueReusableCell(withIdentifier: "emptyCell")
        
        for subV in cell?.contentView.subviews ?? [UIView]() {
            subV.removeFromSuperview()
        }
        cell?.contentView.backgroundColor = .init(hexString: "#F5F6F8")
        
        if indexPath.row == 1 {
//            let containerView = UIView()
//            cell?.contentView.addSubview(containerView)
//            containerView.snp.makeConstraints { make in
//                make.left.equalTo(cell!.contentView).offset(12)
//                make.right.equalTo(cell!.contentView).offset(-12)
//                make.top.equalTo(cell!.contentView)
//                make.bottom.equalTo(cell!.contentView).offset(-10)
//            }
//            containerView.backgroundColor = .white
//            containerView.layer.cornerRadius = 8
//
//            let cellTitle = UILabel()
//            containerView.addSubview(cellTitle)
//            cellTitle.snp.makeConstraints { make in
//                make.left.equalTo(containerView).offset(12)
//                make.top.equalTo(containerView).offset(12)
//                make.height.equalTo(21)
//            }
//            cellTitle.text = "视频展示"
//            cellTitle.font = UIFont.systemFont(ofSize: 15, weight: .semibold)
//            cellTitle.textColor = color("#333333")
//
//            let titleImgV = UIImageView()
//            containerView.addSubview(titleImgV)
//            titleImgV.snp.makeConstraints { make in
//                make.left.equalTo(cellTitle.snp.right).offset(5)
//                make.centerY.equalTo(cellTitle)
//            }
//            titleImgV.contentMode = .scaleToFill
//            titleImgV.image = UIImage(named: "release_video")?.withRenderingMode(.alwaysOriginal)
//
//            self.videoButton = UICloseButton(type: .custom)
//            self.videoButton?.closeCompleted = { [weak self] in
//                self?.videoUrlPath = nil
//            }
//
//            containerView.addSubview(self.videoButton!)
//            self.videoButton?.snp.makeConstraints({ make in
//                make.left.equalTo(containerView).offset(12)
//                make.bottom.equalTo(containerView).offset(-12)
//                make.size.equalTo(CGSize(width: 69, height: 69))
//            })
//            self.videoButton?.setImage(UIImage(named: "add_placeholder"), for: .normal)
//            self.videoButton?.imageView?.contentMode = .scaleToFill
//            self.videoButton?.addTarget(self, action: #selector(selectedMainBoardVideo), for: .touchUpInside)
            
        } else if indexPath.row == 2 {
            
            let containerView = UIView()
            cell?.contentView.addSubview(containerView)
            containerView.snp.makeConstraints { make in
                make.left.equalTo(cell!.contentView).offset(12)
                make.right.equalTo(cell!.contentView).offset(-12)
                make.top.equalTo(cell!.contentView)
                make.bottom.equalTo(cell!.contentView).offset(-10)
            }
            containerView.backgroundColor = .white
            containerView.layer.cornerRadius = 8
            
            let cellTitle = UILabel()
            containerView.addSubview(cellTitle)
            cellTitle.snp.makeConstraints { make in
                make.left.equalTo(containerView).offset(12)
                make.top.equalTo(containerView).offset(12)
                make.height.equalTo(21)
            }
            cellTitle.text = "图片展示"
            cellTitle.font = UIFont.systemFont(ofSize: 15, weight: .semibold)
            cellTitle.textColor = color("#333333")
            
            let titleImgV = UIImageView()
            containerView.addSubview(titleImgV)
            titleImgV.snp.makeConstraints { make in
                make.left.equalTo(cellTitle.snp.right).offset(5)
                make.centerY.equalTo(cellTitle)
            }
            titleImgV.contentMode = .scaleToFill
            titleImgV.image = UIImage(named: "release_image")?.withRenderingMode(.alwaysOriginal)
            
            self.imgButtonOne = UICloseButton(type: .custom)
            self.imgButtonOne?.closeCompleted = { [weak self] in
                self?.removeImgV(index: 0)
            }
            cell?.contentView.addSubview(self.imgButtonOne!)
            self.imgButtonOne?.snp.makeConstraints({ make in
                make.left.equalTo(containerView).offset(12)
                make.bottom.equalTo(containerView).offset(-12)
                make.size.equalTo(CGSize(width: 69, height: 69))
            })
            self.imgButtonOne?.setImage(UIImage(named: "add_placeholder"), for: .normal)
            self.imgButtonOne?.imageView?.contentMode = .scaleToFill
            self.imgButtonOne?.addTarget(self, action: #selector(selectedImgV(_:)), for: .touchUpInside)
            
            self.imgButtonTwo = UICloseButton(type: .custom)
            self.imgButtonTwo?.closeCompleted = { [weak self] in
                self?.removeImgV(index: 1)
            }
            cell?.contentView.addSubview(self.imgButtonTwo!)
            self.imgButtonTwo?.snp.makeConstraints({ make in
                make.left.equalTo(self.imgButtonOne!.snp.right).offset(12)
//                make.centerX.equalTo(cell!.contentView)
                make.bottom.equalTo(containerView).offset(-12)
                make.size.equalTo(CGSize(width: 69, height: 69))
            })
            self.imgButtonTwo?.setImage(UIImage(named: "add_placeholder"), for: .normal)
            self.imgButtonTwo?.imageView?.contentMode = .scaleToFill
            self.imgButtonTwo?.addTarget(self, action: #selector(selectedImgV(_:)), for: .touchUpInside)
            self.imgButtonTwo?.isHidden = true
            
            self.imgButtonThree = UICloseButton(type: .custom)
            self.imgButtonThree?.closeCompleted = { [weak self] in
                self?.removeImgV(index: 2)
            }
            cell?.contentView.addSubview(self.imgButtonThree!)
            self.imgButtonThree?.snp.makeConstraints({ make in
//                make.right.equalTo(containerView).offset(-12)
                make.left.equalTo(self.imgButtonTwo!.snp.right).offset(12)
                make.bottom.equalTo(containerView).offset(-12)
                make.size.equalTo(CGSize(width: 69, height: 69))
            })
            self.imgButtonThree?.setImage(UIImage(named: "add_placeholder"), for: .normal)
            self.imgButtonThree?.imageView?.contentMode = .scaleToFill
            self.imgButtonThree?.addTarget(self, action: #selector(selectedImgV(_:)), for: .touchUpInside)
            self.imgButtonThree?.isHidden = true
            
        } else if indexPath.row == 3 {
            let paddingLabel = JDPaddingLabel()
            paddingLabel.edgeInsets = UIEdgeInsets(top: 12, left: 12, bottom: 12, right: 12)
            cell?.contentView.addSubview(paddingLabel)
            paddingLabel.snp.makeConstraints { make in
                make.left.equalTo(cell!.contentView).offset(12)
                make.top.equalTo(cell!.contentView)
                make.right.equalTo(cell!.contentView).offset(-12)
                make.bottom.equalTo(cell!.contentView).offset(-10)
            }
            paddingLabel.backgroundColor = .white
            paddingLabel.text = "发布规则\n平台收取发布费=0.03/人*圈内总人数\n\n发布费金额\n圈内商讯保留90天，90天后自动下架！\n私聊保留360天，360天后自动下架！"
            paddingLabel.numberOfLines = 0
            paddingLabel.layer.cornerRadius = 8
            paddingLabel.font = UIFont.systemFont(ofSize: 14, weight: .regular)
            paddingLabel.textColor = .init(hexString: "#333333")
            
        }
        return cell!
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        if (indexPath.row == 0 || indexPath.row == 3) {
            return UITableView.automaticDimension
        } else if (indexPath.row == 1 || indexPath.row == 2){
            if indexPath.row == 1 {
                return 0
            }
            return 134
        }
        return 0
    }
    
}

extension JDReleaseUnionNewsViewController {
    override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
        self.view.endEditing(true)
    }
}
